package com.edushare.common.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.query.SelectQuery;
import org.apache.wicket.model.IModel;

import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.ScheduleNotifications;
import com.edushare.common.vo.Schedules;
import com.edushare.common.vo.Users;

public class ScheduleDAO extends CayenneBasedDAO {

	public ScheduleDAO(ORMInterface ormInterface) {
		super(ormInterface);
	}
	
	@SuppressWarnings("unchecked")
	public List<Schedules> getSchedulesByCreatorId(int creatorId, int creatorType){
		Expression exp = Expression.fromString("creatorType = $ctype and schCreator = $creatorid and status = $status");
		HashMap hm = new HashMap();
		hm.put("ctype", creatorType);
		hm.put("creatorid", creatorId);
		hm.put("status", Schedules.EVENT_STATUS_OPEN);
		SelectQuery query = new SelectQuery(Schedules.class, exp.expWithParameters(hm));
		return getDataContext().performQuery(query);
	}
	
	public void commit(){
		getDataContext().commitChanges();
	}
	
	public void addToSchedules(Schedules schedule){
		getDataContext().registerNewObject(schedule);
		commit();
	}	
	
	public void deleteSchedule(Schedules sch){
		getDataContext().deleteObjects(sch.getNotifiedUsers());
		getDataContext().deleteObject(sch);
		getDataContext().commitChanges();
	}
	
	public void sendNotifications(Schedules schedule, List<IModel<Users>> userList, int status){
		if(userList == null)
			return;
		for(IModel<Users> u: userList){
			sendNotifications(schedule, u.getObject(), status);
		}
		commit();
	}
	
	public void sendNotifications(Schedules schedule, Users user, int status){
		ScheduleNotifications notification = new ScheduleNotifications();
		notification.setSchedule(schedule);
		notification.setUser(user);
		notification.setSnType(schedule.getSchType());			
		notification.setActionStatus(status);
		getDataContext().registerNewObject(notification);
	}
	
	
	public void updateNotifications(Schedules schedule){
		List<ScheduleNotifications> sNList = schedule.getNotifiedUsers();
		Iterator<ScheduleNotifications> it = sNList.iterator();		
		while(it.hasNext()){
			it.next().setActionStatus(ScheduleNotifications.CANCELLED);
		}
		commit();
	}
	
	public void clearNotifications(Schedules schedule){
		getDataContext().deleteObjects(schedule.getNotifiedUsers());
		commit();
	}
	
	public List<Users> getRecievers(Schedules schedule){
		List<Users> userList = new ArrayList<Users>();
		List<ScheduleNotifications> sn = schedule.getNotifiedUsers();
		for(ScheduleNotifications s:sn){
			userList.add(s.getUser());
		}
		return userList;
	}
		
	@SuppressWarnings("unchecked")
	public Schedules getScheduleById(Integer id){
		Expression exp = Expression.fromString("schId = $ctype");
		HashMap hm = new HashMap();
		hm.put("ctype", id);
		SelectQuery query = new SelectQuery(Schedules.class, exp.expWithParameters(hm));
		List<Schedules> schList = (List<Schedules>)getDataContext().performQuery(query);		
		if(schList != null && schList.size()>0)
			return (Schedules)schList.get(0);
		else
			return null;
	}
}
